package com.example.shiyan2

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast

class HeadsetDetectReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        var action = intent?.action
        if(Intent.ACTION_HEADSET_PLUG.equals(action)){
            if(intent?.hasExtra("state")!=null){
                val state = intent.getIntExtra("state",0)
                if (state == 1){
                    Toast.makeText(context,"插入耳机",Toast.LENGTH_SHORT).show()
                }else if(state==0){
                    Toast.makeText(context,"拔出耳机",Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}